﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using System.Xml;

namespace Sudum
{
	public class LevelData
	{
		public List<EventData> Events { get; set; }
		public string Name { get; set; }
		public string Model { get; set; }
		public int TimeSecs { get; set; }

		public LevelData()
		{
			Events = new List<EventData>();
		}

		public void Load(string fileName)
		{
			XmlReader xmlReader = null;
			XmlDocument xmlDoc = new XmlDocument();

			try
			{
				xmlReader = new XmlTextReader(fileName);
				xmlDoc.Load(xmlReader);
				xmlReader.Close();

				if (xmlDoc.DocumentElement.Name != "SudumLevel")
					throw new FormatException("Level file is in an invalid format");
			}
			finally
			{
				if (xmlReader != null)
					xmlReader.Close();
			}

			Name = xmlDoc.DocumentElement.Attributes["Name"].Value;
			Model = xmlDoc.DocumentElement.Attributes["Model"].Value;
			TimeSecs = (int)UInt32.Parse(xmlDoc.DocumentElement.Attributes["TimeSecs"].Value);

			foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
			{
				if (node.Name != "Event")
					continue; // ?! Unknown tag... Skip it

				EventData eventData = new EventData();
				eventData.Event = (GameEvent.Type)Enum.Parse(typeof(GameEvent.Type), node.Attributes["Type"].Value);
				eventData.IsRanged = bool.Parse(node.Attributes["IsRanged"].Value);
				eventData.SpawnCount = (int)UInt32.Parse(node.Attributes["SpawnCount"].Value);
				eventData.StartTime = (int)UInt32.Parse(node.Attributes["StartTime"].Value);

				if (eventData.IsRanged)
					eventData.EndTime = (int)UInt32.Parse(node.Attributes["EndTime"].Value);

				Events.Add(eventData);
			}
		}

	#if EDITOR
		public void Save(string fileName)
		{
			XmlWriterSettings settings = new XmlWriterSettings();
			settings.Encoding = Encoding.Unicode;
			settings.Indent = true;
			settings.NewLineOnAttributes = false;

			using (XmlWriter writer = XmlTextWriter.Create(fileName, settings))
			{
				writer.WriteStartDocument(true);
				writer.WriteStartElement("SudumLevel");
				writer.WriteAttributeString("Name", Name);
				writer.WriteAttributeString("Model", Model);
				writer.WriteAttributeString("TimeSecs", TimeSecs.ToString());

				foreach (EventData evt in Events)
				{
					writer.WriteStartElement("Event");
					writer.WriteAttributeString("Type", evt.Event.ToString());
					writer.WriteAttributeString("IsRanged", evt.IsRanged.ToString());
					writer.WriteAttributeString("SpawnCount", evt.SpawnCount.ToString());
					writer.WriteAttributeString("StartTime", evt.StartTime.ToString());
					if (evt.IsRanged)
						writer.WriteAttributeString("EndTime", evt.EndTime.ToString());
					writer.WriteEndElement();
				}

				writer.WriteEndElement();
				writer.WriteEndDocument();
			}
		}
	#endif // EDITOR
	}

	public class GameEvent
	{
		public enum Type
		{
			Shooter,
			Crawler,
			HeavyGuns,
			BossMasterMind,
			BossSpeedster,
			RechargeUnit,
		}

	#if EDITOR
		static GameEvent()
		{
			Types[Type.Shooter] = "القاذف";
			Types[Type.Crawler] = "المتسلق";
			Types[Type.HeavyGuns] = "المدجج";
			Types[Type.BossMasterMind] = "الزعيم الذكي";
			Types[Type.BossSpeedster] = "الزعيم السريع";
			Types[Type.RechargeUnit] = "وحدة شحن سلاح خارق";

			// التحقق من إدخال الأسماء العربية لكل حدث مدعوم
			#if DEBUG
			System.Diagnostics.Debug.Assert(Types.Count == Enum.GetValues(typeof(Type)).Length);
			#endif //DEBUG
		}

		public static Dictionary<Type, string> Types { get { return _Types; } }
		static Dictionary<Type, string> _Types = new Dictionary<Type, string>();
	#endif // EDITOR
	}

	public class EventData
	{
		public int StartTime { get; set; }
		public int EndTime { get; set; }
		public GameEvent.Type Event { get; set; }
		public bool IsRanged { get; set; } // هل الحدث يمتد لفترة زمنية متصلة؟
		public int SpawnCount { get; set; }
	}
}
